<?php

/**
 * @author     Thibaut Cromphaut, Gaïtano Boeraeve, Matthias Mercy
 * @copyright  Copyright (c) 2011 memorygame
 */
class HighscoreController extends Zend_Controller_Action
{

    public function init()
    {
        $this->stylesBase = $this->view->baseUrl() . '/styles/';
    }
    
    public function preDispatch()
    {
        // Stuur de ingelogde gebruiker naar de homepagina, tenzij er uitgelogd wordt.
        if ( Zend_Auth::getInstance()->hasIdentity() ) {
            switch ( $this->getRequest()->getActionName() ) {
                case 'index':
                    break;
                 case 'edit':
                    break;
                case 'delete':
                    break;
                default:
                    $this->_helper->redirector('index', 'index');
                    break;
            }
        }    
    }
    
    public function indexAction()
    {   
        $view = $this->_getView('Highscore');  
        $highscores = $this->_getAllHighscores();  
        $view->highscores = $highscores;
    }
    
    public function editAction()
    {   
        $view = $this->_getView('Edit highscore');
        $form = $this->_getEditHighscoreForm();
                
        if ($this->getRequest()->isPost()) 
	{
            $formData  = $this->_request->getPost();
            if ($form->isValid($formData)) 
            {                
                $view = $this->_getView('Fout - Toevoegen');  
                
                // Promotion toevoegen 
                $values = $form->getValues();

                $highscore_values['id'] = $values['highscore_id'];
                $highscore_values['number'] = $values['highscore_number'];
                $highscore_values['userId'] = $values['users_user_id'];
                $highscore = new Application_Model_Highscore($highscore_values);
                
                $highscoreMapper = new Application_Model_HighscoreMapper();
                $highscoreMapper->save($highscore);

                return $this->_helper->redirector('index', 'highscore');                
            }
        }else{
            $id = (string)$this->getRequest()->getParam('id');
            
            $highscore = $this->_getViewHighscore($id);
            
            $form->populate(
                array(
                    'highscore_id'          => $highscore->Id,
                    'highscore_number'      => $highscore->Number,    
                    'users_user_id'         => $highscore->UserId,
                ));
            
            $view->form = $form; 
        }   
    }
    
    public function deleteAction()
    {
        $id = (string)$this->getRequest()->getParam('id');
        $highscoreMapper = new Application_Model_HighscoreMapper();
        $highscoreMapper->delete($id);
        $this->_helper->redirector('index', 'highscore');
    }

    /**
     * @param string $title
     * @return Zend_View
     */
    private function _getView($title)
    {
        $view = $this->view;
        $view->title = $title;
        $view->headTitle($view->title, 'prepend');                           
        $view->headLink()->appendStylesheet($this->stylesBase . 'main.css'); 

        return $view;
    }
    
    private function _getEditHighscoreForm()
    {   
        return new Application_Form_EditHighscore(array(
            'action' => $this->view->baseUrl() . '/highscore/edit/',
            'post'   => 'post',
        ));
    }
    
    /**
     * @return Array
     */
    private function _getAllHighscores()
    {
        $highscoreMapper = new Application_Model_HighscoreMapper();
        $highscoreArray = $highscoreMapper->fetchAll();

        return $highscoreArray;
    }
    
    private function _getViewHighscore($id)
    {    
        $highscoreMapper = new Application_Model_HighscoreMapper();
        $highscoreArray = $highscoreMapper->fetchById($id);

        return $highscoreArray;
    }
}



